import Vue from "./index.js";
import { isDef, isPrimitive } from "../util/index.js";

export function createEmptyVNode(text) {
  const node = new VNode();
  node.text = text;
  node.isComment = true;
  return node;
}

export class VNode {
  constructor(tag, data, children) {
    this.tag = tag;
    this.data = data;
    this.children = children;
  }
}

const ALWAYS_NORMALIZE = 2;

function createElement(
  context,
  // 标签字符串、组件选项对象
  // 或者是resolve了标签名/选项对象的async函数
  tag,
  // 类似于在标签的属性对象
  data,
  // 子节点（数组或基本类型值）
  children,
  normalizationType,
  alwaysNormalize
) {
  // 说明data是子节点children
  if (Array.isArray(data) || isPrimitive(data)) {
    normalizationType = children;
    children = data;
    data = undefined;
  }
  if (alwaysNormalize === true) {
    normalizationType = ALWAYS_NORMALIZE;
  }
  return _createElement(context, tag, data, children, normalizationType);
}

function _createElement(context, tag, data, children, normalizationType) {
  if (isDef(data) && isDef(data.__ob__)) {
    return createEmptyVNode();
  }
  if (isDef(data) && isDef(data.is)) {
    tag = data.is;
  }
  if (!tag) {
    return createEmptyVNode();
  }
  let vnode;
  if (typeof tag === "string") {
    if (Vue.config.isReservedTag(tag)) {
      vnode = new VNode(tag, data, children, undefined, undefined, context);
    }
  }
  return vnode;
}
